classdef LoopEdit_dev < Processor_dev
  % SeaBird's pressure slow-downs/reversals detection/censor function
  % Derived from Processor_dev
  % Constructor signature: le = LoopEdit_dev(conn, Z, castIndicator), 
  % all args optional
  % TODO: change to arg. optional
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Original release date: N/A

    properties
      % inherited from Processor_dev
      % inData;
      % castDT;
      % scanRate;
      % ParamID;
      % ParamDesc;
      % SBEprec;
      % EquipID;
        DBdata;
        outData;
        P; Z; W;
    end
    
    methods
        function le = LoopEdit_dev(conn, castIndicator, P, lat, win) % Constructor
            global instHeaderData mSB
                
            if nargin < 5, win=3; end
            if nargin < 4, lat=0; end
            if nargin < 3, P = Pressure_dev(); end
            if nargin < 2, castIndicator = zeros(size(P.outData)); end
            if nargin < 1, conn = {}; end
            
            le = le@Processor_dev();
            if ~isempty(conn)
                le.getDBdata(conn, 'LE');
                if ~isempty(P.outData)
                    le.P = P.outData;
                    if mSB, 
                        le.P = round2(le.P, P.SBEprec); 
                    end
                    le.Z = Depth_dev(le.P, lat);
                    le.W = regressDeriv((1:numel(le.P))/instHeaderData.scanRate,...
                                         le.Z, 1:win);
                    if isnumeric(le.W)
                        le.mainFormula(castIndicator, win);
                        if ~mSB
                            le.outData(le.outData == -9.990e-29) = ...
                                                    instHeaderData.BadFlag;
                        end
                    end
                end
            end
        end
        
        function mainFormula(obj, castIndicator, win)
            minVelType = obj.DBdata.LEminVelocityTypeValue{1};
%            xBad = obj.DBdata.LEexcludeMarkedBad{1}; Presently not supported
%            xSoak = obj.DBdata.LEremoveSurfaceSoak{1}; Presently not used
%            useDeckPres = obj.DBdata.LEuseDeckPressure{1}; Presently not used
            obj.outData = zeros(size(castIndicator));
            if minVelType % "Fixed minimum velocity" algorithm
                minCTDvel = obj.DBdata.LEminCTDVelocity{1};
                b = find(castIndicator==0, 1);
                e = find(castIndicator==1, 1) - 1;
                Pmax = obj.P(b); buf = floor(win/2);
                for n = (b+1):(e-1)
                    if (obj.P(n) <= Pmax)
                        obj.outData(n) = -9.990e-29; 
                    end
                    if ~obj.outData(n) && (n - b > buf) && (e - n > buf)
                        if obj.W(n) < minCTDvel
                            obj.outData(n) = -9.990e-29;
                        end
                    end
                    Pmax = max(Pmax, obj.P(n));
                end
                Pmin = obj.P(e); np = numel(obj.P);
                for n = (e+1):np;
                    if (obj.P(n) >= Pmin)
                        obj.outData(n) = -9.990e-29; 
                    end
                    if ~obj.outData(n) && (n - e > buf) && (np - n > buf)
                        if obj.W(n) > -minCTDvel
                            obj.outData(n) = -9.990e-29;
                        end
                    end 
                    Pmin = min(Pmin, obj.P(n));
                end
            else % "Percent of mean speed algorithm"
                % TODO: implement
            end
        end
        
        function delete(obj) % Destructor
            % Here in case I need it
        end
    end
    
end